package com.longrise.android.byjk.plugins.productfeerate;

import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.longrise.common.utils.PrintLog;

/**
 * Created by callxkj on 2017/5/31.
 * 文件名过长的处理方法
 */

public class OnBBfilenameLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
    private static final String TAG = "OnBBfilenameLayoutListener";
    private TextView mTextView;

    public OnBBfilenameLayoutListener(TextView textView) {
        this.mTextView = textView;
    }

    @Override
    public void onGlobalLayout() {
        int lineCount = mTextView.getLineCount();
        PrintLog.d(TAG, "lineCount:" + lineCount);
        if (lineCount > 2) { //这里最多显示两行
            CharSequence ellipsis = "...";
            CharSequence text = mTextView.getText();
            int lineEnd = mTextView.getLayout().getLineEnd(2);
            CharSequence textfirst = text.subSequence(0, lineEnd - 6 - 9);
            CharSequence textend = text.subSequence(text.length() - 9, text.length());
            mTextView.setText(textfirst);
            mTextView.append(ellipsis);
            mTextView.append(textend);
        }

    }
}
